import Vue from "vue";
import Vuex from "vuex";
import socket from "@/socket";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    connected: false, //当前连接状态
    nickname: "", //当前用户昵称
    nicknames: [], //房间用户昵称列表
    holder: "", //游戏主持人
    answerCount: 0, //期望答案的字数
    lines: [], //房间的绘图信息
  },
  mutations: {
    updateConnected(state, connected) {
      state.connected = connected;
    },
    updateNickname(state, nickname) {
      state.nickname = nickname || "";
    },
    updateNicknames(state, nicknames) {
      state.nicknames = nicknames || [];
    },
    updateHolder(state, holder) {
      state.holder = holder || "";
    },
    updateLines(state, lines) {
      state.lines = lines || [];
    },
    updateAnswerCount(state, count) {
      state.answerCount = count;
    },

    addToNicknames(state, nickname) {
      if (!state.nicknames.includes(nickname)) {
        state.nicknames.push(nickname);
      }
    },
    drawNewLine(state, newLine) {
      state.lines.push(newLine);
    },
    updateNewLine(state, lastLine) {
      let line = state.lines[state.lines.length - 1];
      line.points = lastLine.points;
    },
    delFromNicknames(state, nickname) {
      state.nicknames = state.nicknames.filter((item) => item !== nickname);
    },
  },
  actions: {
    //确认用户名是否存在
    checkUserExist(context, nickname) {
      return new Promise((resolve, reject) => {
        socket.emit("check_user_exist", nickname, (isExist) => {
          resolve(isExist);
        });
      });
    },
    //进入房间
    sendUserEnter(context) {
      const nickname = localStorage.getItem("nickname");
      socket.emit("enter", nickname);
      context.commit("updateNickname", nickname);
    },
    //开始游戏申请
    sendStartGame(context, imageAnswer) {
      socket.emit("start_game", imageAnswer);
    },
    //发送提示信息
    sendAdvice(context, advice) {
      socket.emit("give_advice", advice);
    },
    //结束游戏申请
    sendStopGame(context) {
      socket.emit("stop_game");
    },
    //画新线条
    sendDrawNewLine(context, newLine) {
      socket.emit("new_line", newLine);
    },
    sendUpdateNewLine(context, lastLine) {
      socket.emit("update_line", lastLine);
    },
    clearLines(context) {
      socket.emit("clear_line");
    },
    //猜答案
    sendAnswerGame(context, inputImageName) {
      socket.emit("answer_game", inputImageName);
    },
    //玩家离场
    sendUserLeave(context) {
      socket.emit("leave");
      context.commit("updateNickname", "");
      localStorage.removeItem("nickname");
    },
  },
  getters: {
    isGameStarted(state) {
      // 根据主持人是否存在，判断游戏是否开始
      return !!state.holder;
    },
    isGameHolder(state) {
      return state.nickname === state.holder;
    },
  },
});
